package com.itheima.service.impl;

import com.itheima.constant.UserConstant;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;

    @Autowired
    StringRedisTemplate  redisTemplate;

    @Override
    public Result login(String username, String password) {

        User byUserName = userMapper.findByUserName(username);
        if (byUserName == null){
            return Result.error("用户不存在");
        }
        if (!Md5Util.getMD5String(password).equals(byUserName.getPassword())) {
            return Result.error("密码错误");
        }

        //生成jwt的token给前端
        HashMap<String, Object> claim = new HashMap<>();
        claim.put("username",byUserName.getUsername());
        claim.put("id",byUserName.getId());
        String token = JwtUtil.genToken(claim);

        // 60分钟
        redisTemplate.opsForValue().set(UserConstant.LOGIN_PREFIX + byUserName.getId(),token, 60,TimeUnit.MINUTES);

        System.out.println(token);

        return Result.success("登录成功",token);
    }


    @Override
    public Result register(String userName, String password) {

        //判断用户是否存在
        User user = userMapper.findByUserName(userName);
        if (user != null) {
            return Result.error("用户已存在");
        }
        //保存用户
        String pwd = Md5Util.getMD5String(password);
        userMapper.save(userName,pwd);
        return Result.success("注册成功");

    }

    @Override
    public Result getUserInfo() {
        User user = ThreadLocalUtil.get();
        String username = user.getUsername();
        User byUserName = userMapper.findByUserName(username);
        return Result.success(byUserName);
    }

    @Override
    public Result updateUserInfo(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUserInfo(user);
        return Result.success();
    }

    @Override
    public void updateUserPic(String avatar) {
        User user = ThreadLocalUtil.get();
        user.setUserPic(avatar);
        userMapper.updateUserPic(user);
    }

    @Override
    public Result updatePwd(Map<String, String> map) {
        // 校验参数
        String oldPwd = map.get("old_pwd");
        String newPwd = map.get("new_pwd");
        String rePwd = map.get("re_pwd");
        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("参数不能为空");
        }
        User user = ThreadLocalUtil.get();
        User byUserName = userMapper.findByUserName(user.getUsername());
        if (!Md5Util.getMD5String(oldPwd).equals(byUserName.getPassword())) {
            return Result.error("旧密码错误");
        }
        if (!newPwd.equals(rePwd)) {
            return Result.error("两次密码不一致");
        }
        byUserName.setPassword(Md5Util.getMD5String(newPwd));
        userMapper.updatePwd(byUserName);
        // 删除redis中的token
         redisTemplate.delete(UserConstant.LOGIN_PREFIX + user.getId());

        return Result.success("修改成功");
    }
}
